package cn.neusoft.controller;

import cn.neusoft.pojo.LogUser;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(value = "登录的API",description="登录和退出的功能")
@RestController
@RequestMapping("/log")
public class LoginController {

    @ApiOperation(value = "登录功能" )
    public ResultBean login(HttpSession session, @RequestBody LogUser logUser){

        System.out.println("-----LoginController--------login------");
        System.out.println(logUser);

        if("admin".equals(logUser.getUsername()) && "123456".equals(logUser.getPassword())){

            //登录成功 将登录成功的用户信息存入session
            System.out.println("登录成功："+session.getId());
            session.setAttribute("adminUser",logUser);
            return ResultBean.SUCCESS("admin");
        }

        return ResultBean.ERROR("登录失败，用户名或密码错误");
    }

    @ApiOperation(value = "退出登录功能" )
    @GetMapping("/out")     // get + /log/out
    public ResultBean logout(HttpSession session){
        session.removeAttribute("adminUser");
        return ResultBean.SUCCESS();
    }
}
